package com.ildepinc.persistence.concretes.daos;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;

import com.ildepinc.persistence.abstracts.DaoTableGenericExpirable;
import com.ildepinc.persistence.abstracts.TableGenericExpirable;
import com.ildepinc.persistence.concretes.models.Locale;
import com.ildepinc.persistence.interfaces.JdbcConnectionBehaviour;

public class DaoLocale extends DaoTableGenericExpirable {

  public DaoLocale() {
    this.clear();
  }

  public DaoLocale(JdbcConnectionBehaviour connection, String schema) {
    this.clear();
    this.setJdbcConnection(connection);
    this.setTable(new Locale(schema));
    this.setSqlCommand(this.getTable().getSqlCreateTable());
    this.setSqlParameters(new ArrayList<Object>());
  }

  @Override
  public TableGenericExpirable copyRecordTo() {
    Locale locale = (Locale) this.getTable();
    return locale;
  }

  @Override
  public Integer doDelete(HashMap<String, Object> primaryKeys) throws Exception {
    Integer rowsAffected = null;
    this.getSqlParameters().clear();
    this.getSqlParameters().add(primaryKeys.get("LANGUAGE_ID"));
    this.getSqlParameters().add(primaryKeys.get("COUNTRY_ID"));
    this.setSqlCommand(this.getTable().getSqlDelete());
    rowsAffected = this.doManualDmlOrDdl();
    this.setTable(null);
    return rowsAffected;
  }

  @Override
  public Integer doInsert(TableGenericExpirable record, HashMap<String, Object> primaryKeys) throws Exception {
    Integer rowsAffected = null;
    Locale recordToInsert = (Locale) record;
    this.getSqlParameters().clear();
    // if( this.getTable().isPkSingleColumn() ) {
    //   Long newPk = this.getNewProgressive(
    //     this.getTable().getNewProgressiveMethod(),
    //     this.getTable().getSchema(),
    //     this.getTable().getTableName(),
    //     "ID"
    //   );
    //   recordToInsert.getPrimaryKeys().put("ID", newPk);
    //   this.getSqlParameters().add(newPk);
    // }
    if( (primaryKeys != null) && !primaryKeys.isEmpty() ) {
      this.getTable().setPrimaryKeys(primaryKeys);
      primaryKeys.forEach(
        (key, value)
          -> this.getSqlParameters().add(value)
      );
    }
    this.getSqlParameters().add(recordToInsert.getCode());
    this.getSqlParameters().add(recordToInsert.getName());
    this.getSqlParameters().add(recordToInsert.isActive());
    this.getSqlParameters().add(recordToInsert.getValidFrom());
    this.getSqlParameters().add(recordToInsert.getValidTo());
    this.setSqlCommand(this.getTable().getSqlInsert());
    rowsAffected = this.doManualDmlOrDdl();
    this.setTable(recordToInsert);
    return rowsAffected;
  }

  @Override
  public TableGenericExpirable doSelect(HashMap<String, Object> primaryKeys) throws Exception {
    Locale readRecord = null;
    ResultSet resultSet = null;
    this.getSqlParameters().clear();
    this.getSqlParameters().add(primaryKeys.get("LANGUAGE_ID"));
    this.getSqlParameters().add(primaryKeys.get("COUNTRY_ID"));
    this.setSqlCommand(this.getTable().getSqlSelect());
    resultSet = this.doManualSelect();
    resultSet.first();
    this.readRecordFrom(resultSet);
    readRecord = (Locale) this.copyRecordTo();
    this.setTable(readRecord);
    return readRecord;
  }

  @Override
  public Integer doUpdate(TableGenericExpirable record) throws Exception {
    Integer rowsAffected = null;
    Locale recordToUpdate = (Locale) record;
    this.getSqlParameters().clear();
    this.getSqlParameters().add(recordToUpdate.getCode());
    this.getSqlParameters().add(recordToUpdate.getName());
    this.getSqlParameters().add(recordToUpdate.isActive());
    this.getSqlParameters().add(recordToUpdate.getValidFrom());
    this.getSqlParameters().add(recordToUpdate.getValidTo());
    this.getSqlParameters().add(recordToUpdate.getLanguage_id());
    this.getSqlParameters().add(recordToUpdate.getCountry_id());
    this.setSqlCommand(this.getTable().getSqlUpdate());
    rowsAffected = this.doManualDmlOrDdl();
    this.setTable(recordToUpdate);
    return rowsAffected;
  }

  @Override
  public void readRecordFrom(ResultSet resultSet) throws Exception {
    Locale locale = (Locale) this.getTable();
    locale.getPrimaryKeys().put("LANGUAGE_ID", resultSet.getLong("LANGUAGE_ID"));
    locale.getPrimaryKeys().put("COUNTRY_ID", resultSet.getLong("COUNTRY_ID"));
    locale.setCode(resultSet.getString("CODE"));
    locale.setName(resultSet.getString("NAME"));
    locale.setActive(resultSet.getBoolean("ACTIVE"));
    locale.setValidFrom(resultSet.getTimestamp("VALID_FROM"));
    locale.setValidTo(resultSet.getTimestamp("VALID_TO"));
    this.setTable(locale);
  }

  @Override
  public void clear() {
    super.clear();
  }

}
